<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="CHT模号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chtMoldNum">
              <j-search-select-tag placeholder="请输入CHT模号" v-model="model.chtMoldNum" :dictOptions="dictOptions" @change="selectchange()"></j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="进胶方案" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="glueInjectPlan">
              <a-input v-model="model.glueInjectPlan" placeholder="请输入进胶方案"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="流道种类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelType">
              <a-input v-model="model.channelType" placeholder="请输入流道种类"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="模具穴数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldCavitiesNum">
              <a-input v-model="model.moldCavitiesNum" placeholder="请输入模具穴数"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="模具难度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldDifficulty">
              <a-input v-model="model.moldDifficulty" placeholder="请输入模具难度"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="模具寿命（万）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldLife">
              <a-input v-model="model.moldLife" placeholder="请输入模具寿命（万）"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="模具尺寸" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldSize">
              <a-input v-model="model.moldSize" placeholder="请输入模具尺寸"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="顶出方案" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ejectionPlan">
              <a-input v-model="model.ejectionPlan" placeholder="请输入顶出方案"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="热流道品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hotRunnerBrand">
              <a-input v-model="model.hotRunnerBrand" placeholder="请输入热流道品牌"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="热咀数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hotNozzlesNum">
              <a-input v-model="model.hotNozzlesNum" placeholder="请输入热咀数量"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="模胚材质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldMaterial">
              <a-input v-model="model.moldMaterial" placeholder="请输入模胚材质"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="前模芯材质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldCoreMaterial">
              <a-input v-model="model.moldCoreMaterial" placeholder="请输入前模芯材质"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="后模芯材质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldCoreMaterialAfter">
              <a-input v-model="model.moldCoreMaterialAfter" placeholder="请输入后模芯材质"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="模具种类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldType">
              <a-input v-model="model.moldType" placeholder="请输入模具种类"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'YlMoldInfoForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        dictOptions:[],
        testres:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/ch/ylMoldInfo/add",
          edit: "/ch/ylMoldInfo/edit",
          queryById: "/ch/ylMoldInfo/queryById",
          queryAllMoldNum: "ch/ylProjectInfo/queryAllMoldNum",
          queryByChtMoldNum: "/ch/ylProjectInfo/queryByChtMoldNum"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.queryAllMoldNum();
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      queryAllMoldNum(){
        getAction(this.url.queryAllMoldNum).then((res)=>{
              if(res.success){
                this.testres = res.result;
                for(let i = 0;i<this.testres.length;i++){
                  this.dictOptions.push({text:this.testres[i],value:this.testres[i]})
                }
                console.log(this.testres);
              }
            })
      },
      selectchange(){
        console.log(this.model.chtMoldNum);
        getAction(this.url.queryByChtMoldNum,{chtMoldNum:this.model.chtMoldNum}).then((res)=>{
          if(res.success){
            console.log(res.result);
            this.moldBasicInformation = res.result.moldBasicInformation;
            console.log(this.moldBasicInformation);
            
            this.model.glueInjectPlan = this.moldBasicInformation.gateType;
            this.model.channelType = this.moldBasicInformation.runnerType;
            this.model.moldCavitiesNum = this.moldBasicInformation.cavities;
            this.model.moldMaterial = this.moldBasicInformation.moldType;
            this.model.moldCoreMaterialAfter = this.moldBasicInformation.coreSteel;//后模芯
            this.model.moldCoreMaterial = this.moldBasicInformation.cavitySteel;//前模芯
            this.model.moldDifficulty = this.moldBasicInformation.moldDifficulty;
            this.model.moldLife = this.moldBasicInformation.moldLife;
            //this.model.moldDifficulty = this.moldBasicInformation.moldDifficulty;
            //this.model.moldLife = this.moldBasicInformation.moldLife;
            this.model.moldSize = this.moldBasicInformation.moldBaseSize;
            this.model.ejectionPlan = this.moldBasicInformation.ejectorScheme;
            this.model.hotRunnerBrand = this.moldBasicInformation.hotRunnerBrand;
            this.model.hotNozzlesNum = this.moldBasicInformation.numberOfHotNozzle;
            this.$set(this.model,'moldType',this.moldBasicInformation.moldBaseType);
          }
        })
       },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>